import axios from 'axios';
export async function getPoint() {
    const data = [];
    try {
        const response = await axios.get("/api/point/getPoint");
        for (let a = 0; a < response.data.point.length; a++) {
            data.push({
                id:response.data.point[a].id,
                name: response.data.point[a].name,
                lat: response.data.point[a].lat,
                lng: response.data.point[a].lng
            });
        }
    } catch (error) {
        console.error("Error fetching points:", error);
    }
    return data;
}

export async function getAllStatePoint() {
    const data = [];
    try {
        const response = await axios.get("/api/point/getAllPoint");
        for (let a = 0; a < response.data.point.length; a++) {
            data.push({
                id:response.data.point[a].id,
                name: response.data.point[a].name,
                lat: response.data.point[a].lat,
                lng: response.data.point[a].lng,
                state: response.data.point[a].state
            });
        }
    } catch (error) {
        console.error("Error fetching points:", error);
    }
    return data;
}

export async function getLine() {
    const Line = [];
    try {
        const response = await axios.get("/api/point/getLineK");
        for (let a = 0; a < response.data.mst.length; a++) {
            Line.push({
                fromName:response.data.mst[a].fromPoint.name,
                fromLat:response.data.mst[a].fromPoint.lat,
                fromLng: response.data.mst[a].fromPoint.lng,
                toName:response.data.mst[a].toPoint.name,
                toLat: response.data.mst[a].toPoint.lat,
                toLng: response.data.mst[a].toPoint.lng,
                distance: response.data.mst[a].distance
            });
        }
    } catch (error) {
        console.error("Error fetching points:", error);
    }
    return Line
}


export async function getFileName() {
    const fileNames = [];
    try {
        const response = await axios.get("/api/point/getFileNames");
        for (let a = 0; a < response.data.fileNames.length; a++) {
            fileNames.push(response.data.fileNames[a]);
        }
    } catch (error) {
        console.error("Error fetching points:", error);
    }
    return fileNames
}

export async function getFile(fileName) {
    try {
        const response = await axios.get(`/api/point/getLog/${fileName}`, {
            responseType: 'blob'
        });
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', fileName); // 设置下载文件的名称
        document.body.appendChild(link);
        link.click(); // 自动点击链接下载文件
        document.body.removeChild(link); // 下载完成后移除链接
    } catch (error) {
        console.error('Error downloading file:', error);
    }
}

export async function changeState(id,state) {
    try {
        await axios.put(`/api/point/changeState?id=${id}&state=${state}`);
    } catch (error) {
        console.error("Error fetching points:", error);
    }
    return data;
}

export async function addPoint(lat,lng,name) {
    try {
        const response = await axios.post("/api/point/addPoint",{
            lat:lat,
            lng:lng,
            name:name
        });
       return response.data.status;
    } catch (error) {
        console.error("Error fetching points:", error);
    }
}

